<?php

namespace app\api\controller\v1;

use app\common\controller\BaseController;
use app\api\model\RemindOrder as RemindOrderModel;
use app\api\validate\RefundValidate;
use think\Controller;
use think\Db;
use think\Request;
use app\api\model\CourseRefund as CourseRefundModel;
use app\api\model\CourseOrder as CourseOrderModel;
use app\api\model\Course as CourseModel;

class Refund extends BaseController
{
    protected $noNeedLogin = [''];
    /**
     * 退款页面
     */
    public function order()
    {
        $courseOrderId = $this->notEmpty('course_order_id');
        $courseOrder = CourseOrderModel::getByOrderId($courseOrderId);
        if (!$courseOrder) {
            $this->errorMsg(13001);
        }
        $courseOrder['surplus_num'] = count(CourseModel::getSurplusCourseId($courseOrderId));
        $courseOrder['finish_num'] = $courseOrder->num - $courseOrder['surplus_num'];
        $courseOrder['total_fee'] = numberFormat($courseOrder['surplus_num'] * $courseOrder['price']);
        $this->success('', ['course_order' => $courseOrder]);
    }

    /**
     * 提交申请
     */
    public function create(RefundValidate $refundValidate, CourseRefundModel $courseRefundModel)
    {
        $this->isPost();
        $data = $this->request->post();
        $this->paramValidate($refundValidate, $data);
        $courseOrder = CourseOrderModel::find($data['course_order_id']);
        if (!$courseOrder) {
            $this->errorMsg(13001);
        }
        if ($courseOrder->status != 2) {
            $this->errorMsg(13008);
        }
        $courseId = CourseModel::getSurplusCourseId($data['course_order_id']);
        if (!$courseId) {
            $this->errorMsg(13007);
        }
        $data['order_no'] = 'R'.date('YmdHis').$data['course_order_id'].mt_rand(1000, 9999);
        $data['user_id'] = $courseOrder->user_id;
        $data['num'] = count($courseId);
        $data['total_fee'] = $data['num'] * $courseOrder->price;
        Db::startTrans();
        try {
            //创建退款记录
            $courseRefundModel->add($data);
            //更新剩余课次状态
            CourseModel::updateStatus($courseId, 5);
            //更新订单状态
            $courseOrder->status = 4;
            $courseOrder->save();
            //订单提醒
            RemindOrderModel::add($courseOrder, 6);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        isset($e) ? $this->forbidden() : $this->success();
    }

    /**
     * 退款详情
     */
    public function read()
    {
        $courseOrderId = $this->notEmpty('course_refund_id');
        $courseRefund = CourseRefundModel::getByRefundId($courseOrderId);
        $courseRefund['finish_num'] = $courseRefund->course_order->num - $courseRefund->num;
        $courseRefund ? $this->success('', ['course_refund' => $courseRefund]) : $this->errorMsg(13009);
    }

    /**
     * 取消申请
     */
    public function cancel()
    {
        $courseRefundId = $this->notEmpty('course_refund_id');
        $courseRefund = CourseRefundModel::find($courseRefundId);
        if (!$courseRefund) {
            $this->errorMsg(13009);
        }
        if ($courseRefund->status != 1) {
            $this->errorMsg(13010);
        }
        Db::startTrans();
        try {
            //更新退款状态
            $courseRefund->status = 2;
            $courseRefund->save();
            //回退课次状态
            CourseModel::rollbackStatus($courseRefund->course_order_id);
            //回退订单状态
            CourseOrderModel::updateStatus($courseRefund->course_order_id, 2);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        isset($e) ? $this->forbidden() : $this->success();
    }
}
